//
//  HRRetweetView.swift
//  weiBo
//
//  Created by helong on 16/7/14.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit
import SnapKit

class HRRetweetView: UIView {
    //  记录约束
    var retweetViewBottonConstraint: Constraint?
    //  定义属性然后从cell中接收传递过来数据视图中的控件赋值
    var homePageStatusViewModel: HRHomePageStatusViewModel?{
        //  使用属性观察器给 转发微博的文本框 赋值
        didSet{
            retweeted_status.text = homePageStatusViewModel?.retweetContent
            
            retweetViewBottonConstraint?.uninstall()
            if let count = homePageStatusViewModel?.homePageModel?.retweeted_status?.pic_urls?.count where count > 0 {// 如果数组里面有值
                //  传递数据
                retweetCollectionView.pic_urls = homePageStatusViewModel?.homePageModel?.retweeted_status?.pic_urls
                //  显示配图视图
                retweetCollectionView.hidden = false
                //  根据图片张数更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottonConstraint = make.bottom.equalTo(retweetCollectionView).offset(Margin).constraint
                })
            }else{//    如果数组里面没有值,
                //  隐藏配图视图
                retweetCollectionView.hidden = true
                //  根据图片张数更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottonConstraint = make.bottom.equalTo(retweeted_status).offset(Margin).constraint
                })
            }
        
    
        }
    }
    //  懒加载文本框
    private lazy var retweeted_status: UILabel = {
        let lbl = UILabel()
        lbl.textColor = UIColor.darkGrayColor()
        lbl.font = UIFont.systemFontOfSize(HomePageFontSize)
        lbl.numberOfLines = 0
//        lbl.backgroundColor = randomColor()
        printLog()
        return lbl
    }()
    //  懒加载转发视图
    private lazy var retweetCollectionView: HRRetweetCollectionView = {
        let collectionView = HRRetweetCollectionView()
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //  添加子控件,设置约束
        printLog()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     // MARK: - 添加子控件,设置约束
    private func setupUI(){
        addSubview(retweeted_status)
        addSubview(retweetCollectionView)

        //  设置转发内容的约束
        retweeted_status.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(Margin)
            make.trailing.equalTo(self).offset(-Margin)
            make.top.equalTo(self).offset(Margin)
//            make.bottom.equalTo(retweetCollectionView.snp_top).offset(-Margin)
        }
        //  设置转发视图的约束
        retweetCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweeted_status.snp_bottom).offset(Margin)
            make.leading.equalTo(retweeted_status)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
  
        //  关键约束: 设置转发视图的底部约束 = 转发类容的底部 + 间距
        self.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
           self.retweetViewBottonConstraint = make.bottom.equalTo(retweetCollectionView).offset(Margin).constraint
        }
        
    }
}
